package me.qi.kancha.security.grant;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import me.qi.kancha.mapper.AccountMapper;
import me.qi.kancha.dto.core.AccountDTO;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 查找用户接口实现
 *
 * @author sun
 * @date 2019/10/9 15:28
 */
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

    private final AccountMapper accountMapper;

    public UserDetailsServiceImpl(AccountMapper accountMapper) {
        this.accountMapper = accountMapper;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<AccountDTO> queryWrapper = new LambdaQueryWrapper<>();
        AccountDTO accountDTO = accountMapper.selectOne(queryWrapper.eq(AccountDTO::getUsername, username));
        if (accountDTO == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        return accountDTO;
    }
}
